package com.lxinet.jeesns.modules.wechat.web.manage;

import com.lxinet.jeesns.core.dto.ResponseModel;
import com.lxinet.jeesns.core.entity.Page;
import com.lxinet.jeesns.core.web.BaseController;
import com.lxinet.jeesns.modules.wechat.entity.WechatAutoReply;
import com.lxinet.jeesns.modules.wechat.entity.WechatReplyConfig;
import com.lxinet.jeesns.modules.wechat.service.impl.WechatAutoReplyServiceImpl;
import com.lxinet.jeesns.modules.wechat.service.impl.WechatReplyConfigServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lzs on 2017/5/5.
 */
@Controller("wechatAutoReplyController")
@RequestMapping("/")
public class WechatAutoReplyController extends BaseController {

    private static final String MANAGE_FTL_PATH = "/manage/wechatautoreply";

    @Resource
    private WechatReplyConfigServiceImpl wechatReplyConfigService;

    @Resource
    private WechatAutoReplyServiceImpl wechatAutoReplyService;

    @RequestMapping("${managePath}/wechatAutoReply/index")
    public String index(Model model, @RequestParam(value = "wxuin",required = false) String wxuin) {
        List<WechatReplyConfig> wechatReplyConfigList = wechatReplyConfigService.getAll();
        Page page = new Page(request);
        ResponseModel responseModel = wechatAutoReplyService.listByPage(page,wxuin);
        model.addAttribute("list", wechatReplyConfigList);
        model.addAttribute("model",responseModel);
        model.addAttribute("key",wxuin);
        return MANAGE_FTL_PATH + "/index";
    }

    @RequestMapping("${managePath}/wechatAutoReply/list")
    public String wechatReplyConfigList(Model model) {
        List<WechatReplyConfig> wechatReplyConfigList = wechatReplyConfigService.getAll();
        model.addAttribute("list", wechatReplyConfigList);
        return MANAGE_FTL_PATH + "/list";
    }

  @RequestMapping("${managePath}/wechatAutoReply/add")
    public String add(Model model,@RequestParam(value = "wxuin",required = false) String wxuin){
      model.addAttribute("key", wxuin);
      return MANAGE_FTL_PATH + "/add";
    }

    @RequestMapping("${managePath}/wechatAutoReply/save")
    @ResponseBody
    public Object save(WechatAutoReply wechatAutoReply, @RequestParam(value = "wxuin",required = false) String wxuin){
        if(wechatAutoReply == null){
            return new ResponseModel(-2);
        }
        if(StringUtils.isEmpty(wechatAutoReply.getKeyval())){
            return new ResponseModel(-1,"关键字不能为空");
        }
        if(wechatAutoReply.getReply() == null){
            return new ResponseModel(-1,"回复内容不能为空");
        }
        wechatAutoReplyService.save(wechatAutoReply);
        return new ResponseModel(3,"保存成功");
    }

      @RequestMapping("${managePath}/wechatAutoReply/edit/{id}")
     public String edit(@PathVariable("id") int id, Model model){
          WechatAutoReply wechatAutoReply = wechatAutoReplyService.findById(id);
         model.addAttribute("wechatAutoReply",wechatAutoReply);
         return MANAGE_FTL_PATH + "/edit";
     }

     @RequestMapping("${managePath}/wechatAutoReply/update")
     @ResponseBody
     public Object update(WechatAutoReply wechatAutoReply){
         if(wechatAutoReply == null){
             return new ResponseModel(-2);
         }
         WechatAutoReply wrc = wechatAutoReplyService.findById(wechatAutoReply.getId());
         if(wrc == null){
             return new ResponseModel(-1,"配置不存在");
         }
         if(StringUtils.isEmpty(wechatAutoReply.getKeyval())){
             return new ResponseModel(-1,"关键字不能为空");
         }
         if(wechatAutoReply.getReply() == null){
             return new ResponseModel(-1,"回复内容不能为空");
         }
         int flag = wechatAutoReplyService.update(wechatAutoReply);
         if (flag == 1){
             return new ResponseModel(3,"修改成功");
         }
         return new ResponseModel(-1,"修改失败");
     }

    @RequestMapping("${managePath}/wechatAutoReply/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") int id){
        ResponseModel response = wechatAutoReplyService.delete(id);
        return response;
    }
}
